package cn.edu.scau.cmi.zhuangzhihong.hibernate.dao;
// Generated 2019-5-16 18:49:01 by Hibernate Tools 5.2.12.Final

import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;

/**
 * Home object for domain model class Student.
 * @see dao.Student
 * @author Hibernate Tools
 */
public class StudentHome {

	private static final Log log = LogFactory.getLog(StudentHome.class);

	private final SessionFactory sessionFactory = getSessionFactory();
    //添加的
	private final Transaction t = sessionFactory.getCurrentSession().beginTransaction();
	
	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new Configuration().configure().buildSessionFactory();
        }
        catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI"+e.toString());}
	}

	public void comple() {
		t.commit();
	}
	
	//遍历所有
			public List<Student> FindAll() {
				log.debug("Find All Student ");
				try {
					Query query = sessionFactory.getCurrentSession().createQuery("from Student");
					
					List<Student> list= new ArrayList();
					list = query.list();
//					for(Student s : list) {
//						
//						System.out.println(s.toString());
////						list.add(s);
////						
//					}
					log.debug("Find successful");
					return list;
				} catch (RuntimeException re) {
					log.error("persist failed", re);
					throw re;
				}
			}
	
	//插入数据
		public void Save(Student transientInstance) {
			log.debug("persisting Student instance");
			try {
				sessionFactory.getCurrentSession().save(transientInstance);
				log.debug("persist successful");
			} catch (RuntimeException re) {
				log.error("persist failed", re);
				throw re;
			}
		}
		
	//插入数据
	public void persist(Student transientInstance) {
		log.debug("persisting Student instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}
    //更新
	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
//删除
	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public Student getStudent(long SId) {
		log.debug("Get Student instance");
		try {
			Student student=(Student)sessionFactory.getCurrentSession().get(Student.class, SId);
			log.debug("delete successful");
			return student;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
		

		
	}
	
   //更新
	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Student findById(java.lang.Long id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) sessionFactory.getCurrentSession().get("cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.Student", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("cn.edu.scau.cmi.zhuangzhihong.hibernate.dao.Student")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
